package com.test.recordaudio

import android.media.MediaPlayer
import android.net.Uri
import android.os.Bundle
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.test.recordaudio.databinding.ActivityMainBinding
import java.io.File

class MainActivity : AppCompatActivity() {

    private val binding by lazy {
        ActivityMainBinding.inflate(layoutInflater)
    }

    private val mediaFile by lazy {
        File(cacheDir, "test.arm")
    }

    private var isPlaying = false
    private val mediaPlayer by lazy {
        MediaPlayer()
    }

    private val permissionRequest = registerForActivityResult(
        ActivityResultContracts.RequestMultiplePermissions()
    ) { _ ->

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(binding.root)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        initNotificationChannel()
        binding.btnRecord.text = if (RecordService.isRecording) {
            "停止录音"
        } else {
            "开始录音"
        }
        binding.btnRecord.setOnClickListener {
            if (!requestPermission(permissionRequest)) {
                Toast.makeText(this, "请允许权限后再试", Toast.LENGTH_SHORT).show()
            }

            if (isPlaying) {
                Toast.makeText(this, "请先暂停播放后，再录音", Toast.LENGTH_SHORT).show()
            } else if (RecordService.isRecording) {
                RecordService.stop(this)
                binding.btnRecord.text = "开始录音"
            } else {
                RecordService.start(this)
                binding.btnRecord.text = "停止录音"
            }
        }

        binding.btnPlay.setOnClickListener {
            if (RecordService.isRecording) {
                Toast.makeText(this, "请先停止录音后，再点击播放", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }

            if (!mediaFile.exists()) {
                Toast.makeText(this, "没有录音文件，请先录音", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }

            if (!isPlaying) {
                isPlaying = true
                binding.btnPlay.text = "正在-播放录音"
                mediaPlayer.reset()
                mediaPlayer.setDataSource(this, Uri.fromFile(mediaFile))
                mediaPlayer.prepare()
                mediaPlayer.setOnPreparedListener {
                    mediaPlayer.start()
                    binding.btnPlay.text = "停止-播放录音"
                }
                mediaPlayer.setOnCompletionListener {
                    isPlaying = false
                    binding.btnPlay.text = "播放录音"
                    Toast.makeText(this, "播放完毕", Toast.LENGTH_SHORT).show()
                }

            } else {
                isPlaying = false
                mediaPlayer.stop()
                binding.btnPlay.text = "播放录音"
            }
        }
    }
}